﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Relocation.Com.Tools;
using Relocation.Com;
using Relocation.Base;
using Relocation.Data;

namespace Relocation.UI
{
	/// <summary>
	/// 调用方法：
	/// Houses houses = new Houses();
	/// HousesSelect h = new HousesSelect();
	/// h.ShowDialog();
	/// houses = h.houses;
	/// </summary>
	public partial class HousesSelect : HouseListHelp
	{
		public House House;

		public List<int> houseTypes = new List<int>();

		public HousesSelect()
			: base()
		{
			InitializeComponent();
			Initialize();
		}
		public HousesSelect(Session session)
			: base(session)
		{
			InitializeComponent();
			Initialize();
		}


		public HousesSelect(Session session, House house)
			: this(session)
		{
			this.House = house;
			Initialize();

		}
		public HousesSelect(Session session, List<int> _housesTypes)
			: this(session)
		{
			houseTypes = _housesTypes;
			Initialize();
		}



		private void Initialize()
		{
			this.panelSearch.Visible = false;
			this.panelTitle.Visible = false;
			string houseIdQuery = this.House == null ? string.Empty : " || t." + House.GetPropName(t => t.house_id) + " == " + this.House.house_id;
			this.ObjectQuery = this.Session.DataModel.Houses.Where("it." + House.GetPropName(t => t.project_id) + " == " + this.Session.Project.project_id + " &&(it." + House.GetPropName(t => t.isUsed) + " == false " + houseIdQuery + ")");
			this.SortField = House.GetPropName(t=>t.updated);
		}


		//确定选择记录
		private void button_select_Click(object sender, EventArgs e)
		{
			try
			{
				this.House = this.GetSelectEntity();
				if (this.House == null)
					return;
				if (houseTypes != null && (!houseTypes.Contains(this.House.areatype)))
				{
					if (MyMessagebox.Confirm("该户型不是意向安置的户型，确定分配吗？") == DialogResult.No)
						return;
				}
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error(ex.Message);
			}
		}

		/// <summary>
		/// 添加安置房
		/// </summary>		
		private void addHouses_Click(object sender, EventArgs e)
		{
			try
			{
				using (HousesWindow housesInfo = new HousesWindow(this.Session))
				{
					if (housesInfo.ShowDialog(this).IsOK())
						this.Reload();
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				this.MessageBoxShowInfo("操作失败！");
			}			
		}

		/// <summary>
		/// 关闭选择框
		/// </summary>		
		private void close_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}


		/// <summary>
		/// 刷新数据
		/// </summary>
		public override void Refresh()
		{
			base.Refresh();
			this.Reload();
		}

	

		private void housesDataGridView_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode.Equals(Keys.Enter))
				this.button_select.PerformClick();
			e.SuppressKeyPress = false;
		}

		private void housesDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0)
				return;
			this.button_select.PerformClick();
		}

		private void housesDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			if (this.housesDataGridView.Columns[e.ColumnIndex].Name.Equals(this.Column_Index.Name))
			{
				e.Value = (e.RowIndex + 1).ToString();
				e.FormattingApplied = true;
			}
		}

		private void ButtonEdit_Click(object sender, EventArgs e)
		{
			try
			{
				House house = this.GetSelectEntity();
				if (house == null)
					return;
				using (HousesWindow window = new HousesWindow(this.Session, house))
				{
					if (window.ShowDialog(this).IsOK())
						this.Reload();
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				this.MessageBoxShowInfo("操作失败！");
			}
		}
	}
}
